<?php
/**
 * 被观察者
 * User: 16
 * Date: 2018/9/25
 * Time: 14:59
 */
namespace observer;

class Observable implements ObservableInterface{
    /**
     * 观察者们
     * @var array
     */
    private $_observers = [];

    private $_name = "【被观察者：小蘑菇】";

    public function __get($name){
        return $this->$name;
    }

    /**
     * 添加观察者
     * @desc
     * @param ObserverInterface $observer
     * @return mixed
     * @author 16
     * @date 2018/9/25
     */
    public function attach(ObserverInterface $observer){
        if(!in_array($observer,$this->_observers,true)){
            $this->_observers[] = $observer;
        }
    }

    /**
     * 解除观察者
     * @desc
     * @param ObservableInterface $observer
     * @return mixed
     * @author 16
     * @date 2018/9/25
     */
    public function detach(ObservableInterface $observer){
        foreach ($this->_observers as $k => $v){
            if($v === $observer){
                unset($this->_observers[$k]);
            }
        }
    }

    /**
     * 通知观察者
     * @desc
     * @return mixed
     * @author 16
     * @date 2018/9/25
     */
    public function notify(){
        foreach ($this->_observers as $observer){
            $observer->doSomeThing($this);
        }
    }

}